<template>
    <div>
        <div>
            <div class="modal-query">
                <Row :gutter="16">
                    <Col span="5">
                        <Input v-model="projectName" disabled />
                    </Col>
                    <Col span="5">
                        <Input placeholder="工点名称" v-model="workSearchModel.whereMap.name" clearable />
                    </Col>
                    <!-- <Col span="3">
                        <Select v-model="workSearchModel.whereMap.professionalType" placeholder="专业类型" @on-open-change="initProfessionalTypes" ref="professionalTypeSelect">
                            <Option v-for="item in professionalTypesList" :value="item.id" :key="item.id" style="display: none;">{{ item.title }}</Option>
                            <Tree :data="professionalTypes" ref="professionalTypeTree" :load-data="loadData" style="padding-left:20px" @on-select-change="selectProfessionalType"></Tree>
                        </Select>
                    </Col> -->
                    <Col span="3">
                        <Select v-model="workSearchModel.whereMap.riskType" placeholder="风险类型" clearable>
                            <Option v-for="item in riskCategories" :value="item.id" :key="item.id">{{ item.riskCategoryName }}</Option>
                        </Select>
                    </Col>
                    <Col span="3">
                        <Select v-model="workSearchModel.whereMap.distRiskGrade" placeholder="风险等级" clearable>
                            <Option v-for="item in workLevels" :value="item.code" :key="item.id">{{ item.name }}</Option>
                        </Select>
                    </Col>
                    <Col span="5" style="text-align:right;">
                        <Button type="primary" @click="workQuery(1, 10)">查询</Button>
                    </Col>
                </Row>
            </div>
            <div class="modal-table" style="margin-top:20px;">
                <Table class="table" border :columns="workColumns" :data="workList" no-data-text="暂无数据" ref="workTable" @on-selection-change="workSelect">
                    <template slot-scope="{ row }" slot="operation">
                        <a @click="initWorkDetail(row)">详情</a>
                    </template>
                </Table>
                <Row type="flex" justify="center" style="margin-top:20px;">
                    <Col>
                        <Page :page-size-opts='[10,20,50,100]' class="pagination tab-page" :total="workSearchModel.totalRecord" @on-change="pageChange" @on-page-size-change="pageSizeChange" show-elevator show-total show-sizer/>
                    </Col>
                </Row>
            </div>
        </div>
        <div style="text-align:center;margin-top:20px;">
            <Button type="primary" @click="workSave">保存</Button>
            <Button style="margin-left: 20px;" @click="$emit('isGongdian',false)">取消</Button>
        </div>
        <Modal v-model="workDetailModal" title="工点详情" width="70%" height="70%" class="modal" :footer-hide="true">
            <div class="modal-content">
                <div class="app-form">
                    <Form label-position="top">
                        <Row type="flex" justify="space-between">
                            <Col span="11">
                                <FormItem label="项目名称">
                                    <Input v-model.trim="workDetailModel.projectName" disabled />
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="编号">
                                    <Input v-model.trim="workDetailModel.code" disabled />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-between">
                            <Col span="11">
                                <FormItem label="名称">
                                    <Input v-model.trim="workDetailModel.name" disabled />
                                </FormItem>
                            </Col>
                            <!-- <Col span="11">
                                <FormItem label="专业类型">
                                    <Input v-model.trim="workDetailModel.professionalTypeName" disabled />
                                </FormItem>
                            </Col> -->
                            <Col span="11">
                                <FormItem label="风险类型">
                                    <Input v-model.trim="workDetailModel.riskTypeName" disabled />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-between">
                            <Col span="11">
                                <FormItem label="风险等级">
                                    <Input v-model.trim="workDetailModel.distRiskGradeName" disabled />
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label="开始时间">
                                    <Input v-model.trim="workDetailModel.startTime" disabled />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-between">
                            <Col span="11">
                                <FormItem label="结束时间">
                                    <Input v-model.trim="workDetailModel.endTime" disabled />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span="24">
                                <FormItem label="描述备注">
                                    <Input v-model.trim="workDetailModel.note" disabled />
                                </FormItem>
                            </Col>
                        </Row>
                    </Form>
                </div>
            </div>
        </Modal>
    </div>
</template>
<script>
export default {
    props:['projectName','projectId'],
    data(){
        return{
            workDetailModal:false,
            workDetailModel:{},
            // 工点选择查询参数
            workSearchModel: {
                currentPage: 1,
                pageSize: 10,
                whereMap: {
                    projectId: '',
                    name: '',
                    professionalType: '',
                    riskType: '',
                    distRiskGrade: ''
                }
            },
            workList:[],
            professionalTypesList:[],
            professionalTypes: [],
            workSelections:[],
            riskCategories:[],
            workLevels:[],
            workColumns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    width: 70,
                    align: 'center',
                    title: '序号',
                    render: (h, params) => {
                        return h('span', params.index + (this.workSearchModel.currentPage- 1) * this.workSearchModel.pageSize + 1);
                    }
                },
                {
                    title: '名称',
                    align: 'center',
                    key: 'name'
                },
                {
                    title: '风险等级',
                    align: 'center',
                    key: 'distRiskGradeName'
                },
                // {
                //     title: '专业类型',
                //     align: 'center',
                //     key: 'professionalTypeName'
                // },
                {
                    title: '开始时间',
                    align: 'center',
                    key: 'startTime'
                },
                {
                    title: '结束时间',
                    align: 'center',
                    key: 'endTime'
                },
                {
                    title: '操作',
                    slot: 'operation',
                    align: 'center',
                    className: 'table--operation'
                }
            ],
        }
    },
    mounted(){
        this.initRiskCategories()
        this.initWorkLevel()
        this.workQuery()
    },
    methods:{
        initWorkDetail(data){
            this.workDetailModal = !this.workDetailModal
            this.workDetailModel = data
        },
        workQuery(page, size) {
            this.workSearchModel.currentPage = page || 1
            this.workSearchModel.pageSize = size || 10
            this.$api.WorkstationLedger.workstationLedgerList(this.workSearchModel).then(({ code, data, message }) => {
                if (200 === code) {
                    this.workList = data.resultObject
                    this.workSearchModel.totalRecord = data.totalRecord
                } else {
                    this.$Message.error(message)
                }
            })
        },
        // 获取专业类型
        initProfessionalTypes(show) {
            if (show) {
                const param = {
                    currentPage: 1,
                    pageSize: 99999,
                    whereMap: {
                        projectId: this.projectId,
                        parentId: 0
                    }
                }
                this.$api.WorkstationLedger.professionalTypeList(param).then(({ code, data, message }) => {
                    if (200 === code) {
                        data.resultObject.map(r => {
                            this.$set(r, 'title', r.name)
                            if (r.hasChildren) {
                                this.$set(r, 'children', [])
                                this.$set(r, 'loading', false)
                            }
                        })
                        this.professionalTypes = [
                            {
                                title: '标准WBS',
                                parentId: 0,
                                hasChildren: false,
                                loading: false,
                                expand: true,
                                children: data.resultObject
                            }
                        ]
                    } else {
                        this.$Message.error(message)
                    }
                }
                )
            }
        },
        // 选择专业类型
        selectProfessionalType(selected, current) {
            if (current.id) {
                this.professionalTypesList.push({
                    id: current.id,
                    title: current.title
                })
                this.workSearchModel.whereMap.professionalType = current.id
                this.professionalTypeName = current.name
                this.$refs.professionalTypeSelect.visible = false
            }
        },
        // 专业类型树懒加载
        loadData(item, callback) {
            let params = {
                currentPage: 1,
                pageSize: 99999,
                whereMap: {
                projectId: this.projectId,
                parentId: item.id
                }
            }
            this.$api.WorkstationLedger.professionalTypeList(params).then(res => {
                if (res.code == 200) {
                    callback(this.getTree(res.data.resultObject))
                } else {
                    this.$Message.error(res.data.msg)
                }
            })
        },
        getTree(tree) {
            return tree.map(node => {
                node.title = node.name
                if (node.hasChildren) {
                    node.loading = false
                    node.children = []
                }
                return node
            })
        },
        workSelect(selection) {
            this.workSelections = selection
        },
        // 获取风险类型
        initRiskCategories() {
            this.$api.measuresLibrary.getRiskCategoryFindAll().then(({ code, data }) => {
                if (code === 200) {
                    this.riskCategories = data
                }
            })
        },
        // 获取工点风险等级
        initWorkLevel() {
            this.$api.UnitProject.getListByTypeCode('RISK_GRADE').then(resp => {
                if (resp.code == 200) {
                    this.workLevels = resp.data
                } else {
                    this.$Message.warning(resp.message)
                }
            })
        },
        pageChange(page) {
            this.workQuery(page, this.workSearchModel.pageSize)
        },
        pageSizeChange(size) {
            this.workQuery(this.workSearchModel.currentPage, size)
        },
        workSave(){
            if (this.workSelections.length > 0){
                let works = []
                const riskTypes = [
                    ...new Set(this.workSelections.map(e => {
                            return e.id
                        }).filter(t => t)
                    )
                ]
                let params = {
                    idList:riskTypes
                }
                this.$api.WorkstationLedger.getByPointIds(params).then(resp=>{
                    if(resp.code == 200){
                        let result = resp.data
                        this.workSelections.forEach(e => {
                            let type = e.id
                            let temp = type ? result[type] && result[type].length > 0 ? result[type] : [] : []
                            if(temp.length > 0){
                                let risksResult = temp.map(t => {
                                    return {
                                        enterpriseName:t.enterpriseName,
                                        startTime:t.startTime,
                                        endTime:t.endTime,
                                        constructionPointDescription:t.constructionPointDescription,
                                        classificationMethodName:t.classificationMethodName,
                                        classificationMethodId:t.classificationMethodId,
                                        riskDescription:t.riskDescription,
                                        pointRiskGrade:e.distRiskGrade,
                                        pointRiskGradeName: e.distRiskGradeName,
                                        riskOrderId:t.riskOrderId,
                                        projectId:e.projectId,
                                        projectName:e.projectName,
                                        pointId: e.id,
                                        pointName:e.name,
                                        constructionPointId:e.id,
                                        constructionPointName:e.name,
                                        riskCategoryIds:t.riskCategoryIds ? t.riskCategoryIds.split(',') : '',
                                        riskCategoryName:t.riskCategoryNames,
                                        riskCategoryNames:t.riskCategoryNames,
                                        riskLevel:t.riskLevel,
                                        isWork:t.isWorkedThisMonth,
                                        riskOperationId:t.riskOperationId,
                                        riskLevelName:t.riskLevelName,
                                        riskName:t.riskOperationName,
                                        riskOperationName:t.riskOperationName ? t.riskOperationName : '',
                                        isWorkedThisMonthName:t.isWorkedThisMonth,
                                        isFou:'否',
                                        source:'next'
                                    }
                                })
                                works = [...works, ...risksResult]
                            }else{
                                works.push({
                                    classificationMethodId:e.classificationMethodId,
                                    riskDescription:e.riskDescription,
                                    pointRiskGrade:e.distRiskGrade,
                                    pointRiskGradeName: e.distRiskGradeName,
                                    riskOrderId:e.riskOrderId ? e.riskOrderId : '',
                                    projectId:e.projectId,
                                    projectName:e.projectName,
                                    // constructionLevelName:e.distRiskGradeName,
                                    pointId: e.id,
                                    pointName:e.name,
                                    constructionPointId:e.id,
                                    constructionPointName:e.name,
                                    riskCategoryIds:'',
                                    riskCategoryName:'',
                                    riskLevel:e.riskLevel,
                                    isWork:e.work ? '是' : '否',
                                    riskOperationId:e.riskOperationId ? e.riskOperationId : '',
                                    riskLevelName:e.riskLevelName ? e.riskLevelName : '',
                                    riskName:e.riskOperationName ? e.riskOperationName : '',
                                    riskOperationName:e.riskOperationName ? e.riskOperationName : '',
                                    isWorkedThisMonthName:e.isWorkedThisMonth,
                                    isFou:'否',
                                    source:'next'
                                })
                            }
                        })
                        this.$emit('isGongdian',false)
                        this.$emit('gongObj',works)
                    }
                })
            }else{
                this.$Message.warning('请先选择工点')
            }
        }
    }
}
</script>